﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace WeiXinUtils
{
    public class WeiXinBase
    {
        private string m_access_token = string.Empty;
        private int m_expires_in = 0;
        public WeiXinBase(string Appid, string Secret)
        {
            this.appid = Appid;
            this.secret = Secret;
        }

        public string appid { get; set; }
        public string secret { get; set; }
        public string access_token 
        {
            get
            {
                if (string.IsNullOrEmpty(m_access_token))
                {
                    try
                    {
                        string tokenUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
                        dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(HttpsUtils.Get(tokenUrl));
                        m_access_token = result.access_token;
                        m_expires_in = result.expires_in;
                    }
                    catch
                    {
                        m_access_token = string.Empty;
                    }
                }
                return m_access_token;
            }
            set
            {
                m_access_token = value;
            }
        }
        public int expires_in
        {
            get
            {
                return m_expires_in;
            }
            set
            {
                m_expires_in = value;
            }
        }

    }
}